package com.zhida.smartcontrol.pagectrl.projectdetail;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.jay.tools.adapter.MyFragmentAdapter;
import com.jay.tools.base.BaseFragment;
import com.jay.tools.ui.swipemenu.SwipeViewPager;
import com.jay.tools.ui.TopBarView;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.launch.bottom.BottomItemFragment;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.pagectrl.projectdetail.search.ProjectSearchFragment;
import com.zhida.smartcontrol.pagectrl.projectdetail.warning.WarningFragment;
import com.zhida.smartcontrol.pagectrl.projectdetail.zonemanager.ZoneManagerFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * author: tanjianpei
 * date: 2019/10/11 011 11:10
 * description: 区段管理，故障报警
 */
public class ProjectDetailFragment extends BottomItemFragment<ProjectDetailContract.View, ProjectDetailPresenter> implements ProjectDetailContract.View {
    public static final int FROM_HOME = 0; // 首页点击进入
    public static final int FROM_CTRL = 1; // 智控列表点击进入
    public static final int FROM_LOGIN = 2; // 是否是首页

    @BindView(R.id.top)
    TopBarView mTopBar;
    @BindViews({R.id.tv_qy, R.id.tv_gz})
    List<TextView> mTvTitles;
    @BindViews({R.id.view_qy, R.id.view_gz})
    List<View> mViews;
    @BindView(R.id.iv_exp)
    ImageView mIvExp;

    private boolean mExp = true;
    private List<BaseFragment> mFragmentList;

    public static ProjectDetailFragment newInstance(String title, String projectId, int from) {
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putInt("from", from);
        args.putString("id", projectId);
        ProjectDetailFragment fragment = new ProjectDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }
int from;
    @Override
    protected Object getLayout() {
        return R.layout.fragment_project_detail;
    }
    @Override
    protected void initLazy(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        String projectName = bundle.getString("title");

//        inflateTopBar(mTopBar, projectName);
         from = bundle.getInt("from");
        mTopBar.setTitle(projectName);
        mTopBar.setOnTopBarClickListener(new TopBarView.OnTopBarClick() {
            @Override
            public void leftClick() {
                pop();
            }

            @Override
            public void rightClick() {
                open(ProjectSearchFragment.newInstance(bundle.getString("id"),projectName),from);
            }
        });

        int index=0;
        if (from!=FROM_LOGIN) {
            index = from == FROM_HOME ? 1 : 0;
            clickTabGuZhang(from == FROM_HOME);
        }else {
            mTopBar.getmLeftTextView().setVisibility(View.GONE);
        }

        mFragmentList = new ArrayList<>();
        mFragmentList.add(ZoneManagerFragment.newInstance(bundle.getString("id"), projectName).setListener(new StartClickListener() {
            @Override
            public void start(BaseFragment fragment) {
                open(fragment,from);
            }
        }));
        mFragmentList.add(WarningFragment.newInstance(bundle.getString("id"), projectName));

        final SupportFragment[] fragmentArray = mFragmentList.toArray(new SupportFragment[mFragmentList.size()]);
        loadMultipleRootFragment(R.id.fl_container, index, fragmentArray);
    }

    @OnClick({R.id.tv_qy, R.id.tv_gz, R.id.iv_exp})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_qy:
                clickTabGuZhang(false);
                showHideFragment(mFragmentList.get(0), mFragmentList.get(1));
                break;
            case R.id.tv_gz:
                clickTabGuZhang(true);
                showHideFragment(mFragmentList.get(1), mFragmentList.get(0));
                break;
            case R.id.iv_exp:
                if (mExp) {
                    mIvExp.setImageResource(R.drawable.ic_exp_left);
                } else {
                    mIvExp.setImageResource(R.drawable.ic_exp);
                }
                mExp = !mExp;
                ((ZoneManagerFragment) mFragmentList.get(0)).exp();
                break;
        }
    }

    @Override
    public boolean onBackPressedSupport() {
        if (from!=FROM_LOGIN){
            return false;
        }else {
            return super.onBackPressedSupport();
        }
    }

    /**
     * @param isGz 是否选中故障标签
     */
    private void clickTabGuZhang(boolean isGz) {
        mTvTitles.get(isGz ? 1 : 0).setEnabled(false);
        mViews.get(isGz ? 1 : 0).setBackgroundResource(R.color.c_3862FF);
        mTvTitles.get(isGz ? 0 : 1).setEnabled(true);
        mViews.get(isGz ? 0 : 1).setBackgroundResource(android.R.color.transparent);
    }

    private void open(BaseFragment fragment,int from) {
        if (from==FROM_LOGIN) {
            getParentDelegate().start(fragment);
        }else {
            start(fragment);

        }
    }

}
